<template>
  <div>
    <headerMidder title="我的关注"></headerMidder>
    <div class="item" v-for="(item,index) in myFollow" :key="index">
      <img class="avatar" :src="item.head_img" alt />
      <div class="itemMiddle">
        <div class="name">{{item.nickname}}</div>
        <div class="date">2019-10-10</div>
      </div>
      <div class="btnCancer" @click="cancelFollow(item.id)">取消关注</div>
    </div>
  </div>
</template>

<script>
import headerMidder from "../components/headerMidder";
export default {
  components: {
    headerMidder
  },
  data() {
    return {
      myFollow: {}
    };
  },
  methods: {
      loadPage(){
      this.$axios({
      url: "/user_follows",
      method: "get"
    }).then(res => {
      console.log(res.data);
      this.myFollow = res.data.data;
      this.myFollow.forEach(element => {
        if (!element.head_img) {
          element.head_img = "/static/imgs/avatar.jpg";
        } else {
          element.head_img = this.$axios.defaults.baseURL + element.head_img;
        }
      });
    });
      },
    cancelFollow(id) {
      // 我们需要取消关注一个用户那么就需要他的 id
      // 发送ajax 请求
      this.$axios({
        url: "/user_unfollow/" + id,
        method: "get"
      }).then(res => {
        console.log(res.data);
        // 取消关注已经成功了,刷新页面
        this.loadPage();
      });
    }
  },
  mounted() {
    this.loadPage()
  }
};
</script>

<style lang="less" scoped>
.item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #eee;
}
.avatar {
  width: 11.111vw;
  border-radius: 50%;
}
.itemMiddle {
  flex: 1;
  text-align: left;
  padding-left: 2.778vw;
  font-size: 15px;
  .date {
    font-size: 14px;
    color: #888;
  }
}
.name {
  margin-bottom: 1.389vw;
}
.btnCancer {
  width: 20.833vw;
  line-height: 8.333vw;
  height: 8.333vw;
  text-align: center;
  border-radius: 15px;
  background: red;
  color: #eee;
  font-size: 14px;
}
</style>